#ifndef HTTP_CONN_H
#define HTTP_CONN_H

#include <sys/types.h>
#include <sys/uio.h>
#include <arpa/inet.h>
#include <stdlib.h>
#include <errno.h>

#include "../Log/log.h"
#include "../Pool/sqlconnpool.h"
#include "../Buffer/buffer.h"
#include "httprequest.h"
#include "httpresponse.h"

class HttpConn
{
public:
    HttpConn();
    ~HttpConn();

    void init(int sockFd, const sockaddr_in& addr); //创建socket连接初始化

    ssize_t read(int* saveError);

    ssize_t write(int* saveError);

    void Close();

    int GetFd() const;  //获取文件描述符

    int GetPort() const;    //获取通信端口

    const char* GetIP() const;  //获取IP

    sockaddr_in GetAddr() const;

    bool process();

    int ToWriteBytes()
    {
        return iov_[0].iov_len + iov_[1].iov_len; //所有的写的数据
    }

    bool IsKeepAlive() const
    {
        return request_.IsKeepAlive();
    }

    static bool isET;
    static const char* srcDir;              // 资源目录
    static std::atomic<int> userCount;      // 总共的客户端连接数

private:
    int fd_;
    struct sockaddr_in addr_;  //通信的地址

    bool isClose_;

    int iovCnt_;
    struct iovec iov_[2];

    Buffer readBuff_;       // 读（请求）缓冲区，保存请求数据的内容
    Buffer writeBuff_;      // 写（响应）缓冲区，保存响应数据的内容

    HttpRequest request_;
    HttpResponse response_;
};


#endif